package com.bhsy.hellobootspring.servlet;

import com.bhsy.hellobootspring.SpringContextUtil;
import com.bhsy.hellobootspring.controller.LoginController;
import com.bhsy.hellobootspring.model.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
      {
        doPost(req,resp);
      }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
      {
          User user = new User(req.getParameter("username"),req.getParameter("password"));
          System.out.println(user);
          LoginController loginController = (LoginController)SpringContextUtil.getBean("login");
          if(loginController.Login(user))
            {
              req.setAttribute("user",user);
              req.setAttribute("show1","none");
              req.setAttribute("show2","block");
              req.setAttribute("show3","none");
            }
          else
            {
              req.setAttribute("show1","none");
              req.setAttribute("show2","none");
              req.setAttribute("show3","block");
            }
          req.getRequestDispatcher("/index").forward(req,resp);
      }
}
